package cn.edu.gzhu.workOrderManagement.intercepter;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.enumeration.UserAuthorityEnum;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.WorkOrderListDto;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderDetailVo;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderVo;
import cn.edu.gzhu.workOrderManagement.service.WorkOrderService;
import cn.edu.gzhu.workOrderManagement.util.ResponseUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//总部负责人、总部商务、总部稽核查看所有工单
//分支机构只能查看本机构的工单
//分支机构主管只能查看本人处理的工单和业务员处理的工单
//业务员只能查看本人处理的工单
//客户只能查看本人创建的工单
@Component
@Slf4j
public class WorkOrderListInterceptor implements HandlerInterceptor {
    @Autowired
    WorkOrderService workOrderService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Claims claims =(Claims) request.getAttribute("claims");
        String username = claims.get("username").toString();
        String organization = claims.get("organization").toString();
        UserAuthorityEnum authority = UserAuthorityEnum.value2Object(claims.get("authority").toString());
        log.info(authority.getRole(),"{}:在尝试查看工单列表");
        //总部负责人、总部商务、总部稽核查看所有工单
        if (UserAuthorityEnum.HEAD_BUSINESS.equals(authority)
                ||UserAuthorityEnum.HEAD_LEADER.equals(authority)
                ||UserAuthorityEnum.HEAD_AUDITORS.equals(authority)) {
            return true;
        }

        String uuid = request.getParameter("uuid");

        String targetOrganization;
        String targetUser;
        if (uuid == null|| uuid.isEmpty()) {
            targetOrganization =request.getParameter("targetOrganization");
            targetUser = request.getParameter("targetUser");
        }
        else {
            IPage<WorkOrderVo> workOrderDetailVoIPage = workOrderService.listWorkOrder(WorkOrderListDto.builder().uuid(uuid).startPage(1).pageSize(10).needDetails(true).build());
            List<WorkOrderVo> records = workOrderDetailVoIPage.getRecords();
            WorkOrderDetailVo workOrderDetailVo = (WorkOrderDetailVo) records.get(0);
            targetOrganization = workOrderDetailVo.getTargetOrganization();
            targetUser = workOrderDetailVo.getTargetUser();
        }

        if (authority.equals(UserAuthorityEnum.CLIENT)){
            if (!username.equals(request.getParameter("requestUser"))) {
                ResponseUtil.failResponse(response, MessageConstant.PERMISSION_DENIED);
                log.info(authority.getRole(),"{}:查看工单列表权限校验失败");
                return false;
            }
            return true;
        }
        //分支机构只能查看本机构工单
        if (targetOrganization==null||targetOrganization.isEmpty()||!targetOrganization.equals(organization)) {
            ResponseUtil.failResponse(response, MessageConstant.PERMISSION_DENIED);
            log.info(authority.getRole(),"{}:查看工单列表权限校验失败");
            return false;
        }
        //主管、工单处理员、材料处理员只能查看自己的要处理的工单
        if (authority.equals(UserAuthorityEnum.BRANCH_SUPERVISOR)
                || authority.equals(UserAuthorityEnum.BRANCH_WORK_HANDLER)
                || authority.equals(UserAuthorityEnum.BRANCH_MATERIAL_HANDLER)) {

            if (!targetUser.equals(username)) {
                ResponseUtil.failResponse(response, MessageConstant.PERMISSION_DENIED);
                log.info(authority.getRole(),"{}:查看工单列表权限校验失败");
                return false;
            }
        }

        return true;

    }


}
